package io.github.uicdb.et.data;

import lombok.extern.slf4j.Slf4j;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.server.packs.PackType;
import net.minecraft.world.effect.MobEffect;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.data.event.GatherDataEvent;

import java.util.concurrent.CompletableFuture;

@Slf4j
@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD)
public class Datagens {
    @SubscribeEvent
    public static void run(GatherDataEvent event) {
        dumpNoIcons(event.getLookupProvider(), event.getExistingFileHelper());
        event.getGenerator()
                .addProvider(event.includeServer(), new ETRepiceProvider(
                        event.getGenerator().getPackOutput(), event.getLookupProvider()
                ));
        event.getGenerator().addProvider(
                event.includeClient(), new ETBlockStateProvider(event)
        );
        event.getGenerator().addProvider(
                event.includeClient(), new ETtemModelProvider(event)
        );
        var blockTags = new ETBlockTagsProvider(event);
        event.getGenerator().addProvider(event.includeServer(), blockTags);
        event.getGenerator().addProvider(
                event.includeServer(), new ETItemTagsProvider(event, blockTags)
        );
        event.getGenerator().addProvider(
                event.includeClient(), new ZHLangProvider(event.getGenerator().getPackOutput())
        );
    }

    private static void dumpNoIcons(CompletableFuture<HolderLookup.Provider> lookupProvider, ExistingFileHelper existingFileHelper) {
        StringBuilder sb = new StringBuilder("\n");
        for (MobEffect mobEffect : BuiltInRegistries.MOB_EFFECT) {
            var key = BuiltInRegistries.MOB_EFFECT.getKey(mobEffect);
            boolean exist = existingFileHelper.exists(key, PackType.CLIENT_RESOURCES, ".png", "textures/mob_effect");
            if (!exist) {
                sb.append(key).append("\n");
            }
            log.debug("key={}, exist={}", key, exist);
        }
        log.warn(sb.toString());
    }

}
